package servlet;

import biz.BookService;
import biz.BookServiceImpl;
import biz.BookSortService;
import biz.BookSortServiceImpl;
import com.alibaba.fastjson.JSON;
import pojo.BookSort;
import utils.Constants;
import utils.Page;
import vo.BookVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

@WebServlet(name = "BookSortServlet", value = "/getBookSorts")
public class BookSortServlet extends HttpServlet {

    BookSortService sortService =null;
    public BookSortServlet() {
        sortService =new BookSortServiceImpl();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        String opr = request.getParameter("opr");

        try {
            if (null==opr || "".equals(opr)) opr="sortList";

            if ("sortList".equals(opr)){
                try {
                    List<BookSort> sortList=sortService.getBookSortList(null);
                    out.print(JSON.toJSONString(sortList));
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        } catch (Exception throwables) {
            throwables.printStackTrace();
        } finally {
            out.flush();
            out.close();
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
